Skip to content

INTPYTHON-624 Add PolymorphicEmbeddedModelField #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

timgraham
Copy link
Collaborator

@timgraham timgraham commented Jun 25, 2025

No description provided.

if transform:
return transform
field = None
for model in self.embedded_models:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if multiple submodes has the same field name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I called this out in the design doc. I think we'll have to use the system check framework to enforce that common field names at least use the same type, otherwise, we won't know how to prepare the lookup value (e.g. the DatabaseOperations.adapt_<field>_value() methods).

Thinking about it some more, there is also the possibility that nested embedded documents share a field name. In that case, we won't know which field to traverse for the nested lookups that follow.

@@ -121,8 +121,10 @@ def _isnull_operator(a, b):
"gte": lambda a, b: {"$gte": [a, b]},
# MongoDB considers null less than zero. Exclude null values to match
# SQL behavior.
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, {"$ne": [a, None]}]},
"lte": lambda a, b: {"$and": [{"$lte": [a, b]}, {"$ne": [a, None]}]},
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, DatabaseWrapper._isnull_operator(a, False)]},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a regression in model_fields_.test_embedded_model_array.QueryingTests.test_array_as_rhs_for_arrayfield_lookups) (lookup='lt'):

pymongo.errors.OperationFailure: Expression $type takes exactly 1 arguments. 2 were passed in., full error: {'ok': 0.0, 'errmsg': 'Expression $type takes exactly 1 arguments. 2 were passed in.', 'code': 16020, 'codeName': 'Location16020'}

Probably an array literal is treated as a multi-argument value in "$eq": [{"$type": a}, "missing"]},

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems, If I don't remember wrong, the Value(value) is transformed in {"$literal": value}. So the $eq should work. I have to debug this particular case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the literal literally fixes the issue.
In django_mongodb_backend/expressions.py, make the following change:

--- a/django_mongodb_backend/expressions.py
+++ b/django_mongodb_backend/expressions.py
@@ def value(self, compiler, connection):  # noqa: ARG001
     value = self.value
-    if isinstance(value, int):
-        # Wrap numbers in $literal to prevent ambiguity when Value appears in
+    if isinstance(value, int | list):
+        # Wrap numbers and list in $literal to prevent ambiguity when Value appears in
         # $project.
         return {"$literal": value}

@timgraham timgraham marked this pull request as ready for review July 1, 2025 01:13
)
field_name = field.name
if existing_field := embedded_fields.get(field.name):
connection = _get_mongodb_connection()
Copy link
Collaborator

@WaVEV WaVEV Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should run anytime? or only when a migration is applied on the database? 🤔
Given that make migrations does not pass the database, because it should be agnostic to that but when a migration is applied it should not be agnostic. So when do we want to check this? when a migration is created, or anytime?

Edit: change the way that field types are compared is viable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants